#include "NetCalculator.hpp"
#include "Protocol.hpp"
#include "TcpServer.hpp"
#include <memory>
#include <cstdio>
void Usage(string proc)
{
    cerr << "Usage: " << proc << " port" << endl;
}


// ./tcpserver 8080
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERRO);
    }

    // 1. 顶层
    unique_ptr<Calculator> cal = make_unique<Calculator>();

    // 2. 协议层
    unique_ptr<Protocol> protocol = make_unique<Protocol>([&cal](Request &req)->Respond{
        return cal->Execute(req);
    });

    // 3. 服务器层
    // unique_ptr<TcpServer> tsvr = make_unique<TcpServer>(stoi(argv[1]),
    //     [&protocol](shared_ptr<Socket> &sock, INETADDR &client){
    //         protocol->GetRequest(sock, client);
    // });
    unique_ptr<TcpServer> tsvr = make_unique<TcpServer>(8081,
        [&protocol](shared_ptr<Socket> &sock, INETADDR &client){
            protocol->GetRequest(sock, client);
    });

    tsvr->Start();

    return 0;
}